{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ed43bbc3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DD is now sitting.\n",
      "DD rolled over!\n",
      "<class '__main__.Dog'>\n",
      "DD\n",
      "3\n",
      "--------------\n",
      "['_Dog__age', '_Dog__name', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'roll_over', 'sit']\n"
     ]
    }
   ],
   "source": [
    "class Dog:\n",
    "    \"\"\"一个狗类\"\"\"\n",
    "    def __init__(self, name, age=3):\n",
    "        \"\"\"构造器\"\"\"\n",
    "        self.__name = name\n",
    "        self.__age = age\n",
    "    def sit(self):\n",
    "        \"\"\"坐下\"\"\"\n",
    "        print(f\"{self.__name} is now sitting.\")\n",
    "    def roll_over(self):\n",
    "        \"\"\"打滚\"\"\"\n",
    "        print(f\"{self.__name} rolled over!\")\n",
    "\n",
    "dog = Dog(\"DD\",2)\n",
    "\n",
    "dog.sit()\n",
    "dog.roll_over()\n",
    "print(type(dog))\n",
    "\n",
    "dog = Dog(\"DD\")\n",
    "print(dog._Dog__name)\n",
    "print(dog._Dog__age)\n",
    "\n",
    "print(\"--------------\")\n",
    "print(dir(dog))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "803efb4b",
   "metadata": {},
   "outputs": [],
   "source": [
    " class Car:\n",
    "    \"\"\"一次模拟汽车的简单尝试。\"\"\"\n",
    "    def __init__(self, make, model, year):\n",
    "        self.make = make\n",
    "        self.model = model\n",
    "        self.year = year\n",
    "        self.odometer_reading = 0\n",
    "    def get_descriptive_name(self):\n",
    "        long_name = f\"{self.year} {self.make} {self.model}\"\n",
    "        return long_name.title()\n",
    "    def read_odometer(self):\n",
    "        print(f\"This car has {self.odometer_reading} miles on it.\")\n",
    "    def update_odometer(self, mileage):\n",
    "        if mileage >= self.odometer_reading:\n",
    "            self.odometer_reading = mileage\n",
    "        else:\n",
    "            print(\"You can't roll back an odometer!\")\n",
    "    def increment_odometer(self, miles):\n",
    "                self.odometer_reading += miles\n",
    "\n",
    "class ElectricCar(Car):\n",
    "    \"\"\"电动汽车的独特之处。\"\"\"\n",
    "    def __init__(self, make, model, year):\n",
    "        \"\"\"初始化父类的属性。\"\"\"\n",
    "        super().__init__(make, model, year)\n",
    "        my_tesla = ElectricCar('tesla', 'model s', 2019)\n",
    "        print(my_tesla.get_descriptive_name())\n",
    "        \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
